<?php


namespace App\Models;


use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasDateTimeFormatter;
    protected $softDeleteField = 'is_delete';

    protected $casts = [
        'is_delete' => 'integer',
        'sku' => 'array',
    ];

    protected static function booted()
    {
        static::addGlobalScope('not_deleted', function (Builder $builder) {
            $builder->where('is_delete', 0);
        });
    }

    public function delete()
    {
        $this->is_delete = 1;
        return $this->save();
    }

    public function trashed()
    {
        return $this->is_delete === 1;
    }


    public function categorys()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }


    public function getCoverAttribute($value)
    {
        if (!$value) return null;
        $domain = (strstr($value,'/cv/')||strstr($value,'/new_cms_img/')) ? env('IMAGE_URL_NEW') : env('IMAGE_URL');
        if (str_starts_with($value, 'http')){
            return $value;
        }

        if (str_starts_with($value, '/storage')) {
            $value = substr($value, 8);

        }
        return $domain . $value;
    }

    public function getContentCoverAttribute($value)
    {
        if (!$value) return null;
        $domain = (strstr($value,'/cv/')||strstr($value,'/new_cms_img/')) ? env('IMAGE_URL_NEW') : env('IMAGE_URL');
        if (str_starts_with($value, 'http')){
            return $value;
        }

        if (str_starts_with($value, '/storage')) {
            $value = substr($value, 8);

        }
        return $domain . $value;
    }

    public function getVideoCoverAttribute($value)
    {
        if (!$value) return null;
        $domain = (strstr($value,'/cv/')||strstr($value,'/new_cms_img/')) ? env('IMAGE_URL_NEW') : env('IMAGE_URL');
        if (str_starts_with($value, 'http')){
            return $value;
        }

        if (str_starts_with($value, '/storage')) {
            $value = substr($value, 8);

        }
        return $domain . $value;
    }
}
